package com.example.hotelback.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.hotelback.entity.Params;
import com.example.hotelback.entity.Reservation;
import com.example.hotelback.entity.User;
import com.example.hotelback.service.ReservationService;
import com.example.hotelback.utils.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reservation")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    @ApiOperation("查看所有订单")
    @GetMapping("/findAll")
    @ResponseBody
    public Result findAll() {
        List<Reservation> list = reservationService.findAll();
        return Result.success(list);
    }


    /**
     * 分页查询所有订单
     * @param params
     * @return
     */
    @ApiOperation("分页查询所有订单")
    @GetMapping("/findByPage")
    @ResponseBody
    public Result findByPage(Params params) {
         IPage<Reservation> ipage = reservationService.findByPage(params);
         return Result.success(ipage);
    }

    /**
     * 用户查看个人订单
     * @param id
     * @return
     */
    @ApiOperation("查看个人订单")
    @GetMapping("/find")
    @ResponseBody
    public Result find(int id) {
        List<Reservation> reservations = reservationService.find(id);
        return Result.success(reservations);
    }

    /**
     * 用户删除个人订单
     * @param id
     * @return
     */
    @ApiOperation("删除订单")
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody Map<String, Integer> params) {
        int id = params.get("id");
        reservationService.deleteReservation(id);
        return Result.success();
    }

    /**
     * 用户提交订单
     * @param userId
     * @param roomId
     * @param params
     * @return
     */
    @ApiOperation("新增订单")
    @PostMapping("/add")
    @ResponseBody
//    public Result add(int userId, int roomId, Params params) {
    public Result add(@RequestBody Map<String, Object> payload) {
        Integer userId = (Integer) payload.get("uid");
        Integer roomId = (Integer) payload.get("rid");
        Params params = new ObjectMapper().convertValue(payload.get("params"), Params.class);
        reservationService.addReservation(userId, roomId, params);
        return Result.success();
    }
}
